﻿using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.ServiceModel.Channels;
using System.Web;
using System.Web.Http;
using System.Web.Http.ExceptionHandling;

using Autofac;
namespace Zeus
{
    using Logger;
    using DependencyInjection;
    public static class ZeusExtension
    {

        /// <summary>
        /// 配置ExceptionHandler
        /// </summary>
        /// <param name="configuration"></param>
        /// <param name="exceptionHandler"></param>
        public static void ConfigExceptionHandler(this HttpConfiguration configuration, IExceptionHandler exceptionHandler)
        {
            configuration.Services.Replace(typeof(IExceptionHandler), exceptionHandler);
        }

        // <summary>
        /// 获取IP地址
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static string GetClientIp(this HttpRequestMessage request)
        {
            if (request.Properties.ContainsKey("MS_HttpContext"))
            {
                return ((HttpContextWrapper)request.Properties["MS_HttpContext"]).Request.UserHostAddress;
            }
            if (request.Properties.ContainsKey(RemoteEndpointMessageProperty.Name))
            {
                RemoteEndpointMessageProperty remoteEndpointMessageProperty = (RemoteEndpointMessageProperty)request.Properties[RemoteEndpointMessageProperty.Name];
                return remoteEndpointMessageProperty.Address;
            }
            return null;
        }

        /// <summary>
        /// 获取Header
        /// </summary>
        /// <param name="headers"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string GetHeader(this HttpRequestHeaders headers, string name)
        {
            IEnumerable<string> values = null;
            if (headers.TryGetValues(name, out values))
            {
                var valueEnumerator = values.GetEnumerator();
                valueEnumerator.MoveNext();
                return valueEnumerator.Current;
            }
            return string.Empty;
        }
        public static void EnableZLogger(this HttpConfiguration configuration, IZLogger zlogger, IZLoggerContext zLoggerContext = null)
        {
            var dependencyResolver = InitDependencyResolver(configuration);
            dependencyResolver.ContainerBuilder.RegisterInstance(zlogger).As<IZLogger>();
            zLoggerContext = zLoggerContext == null ? new DefaultZLoggerContext() : zLoggerContext;
            dependencyResolver.ContainerBuilder.RegisterType(zLoggerContext.GetType()).As<IZLoggerContext>();
            configuration.MessageHandlers.Add(new ZLoggerMessageHandler());
        }

        public static void BuildDependencyContainer(this HttpConfiguration configuration)
        {
            InitDependencyResolver(configuration).Build();
        }

        private static AutofacWebApiDependencyResolver InitDependencyResolver(HttpConfiguration configuration)
        {
            if (configuration.DependencyResolver == null || !(configuration.DependencyResolver is AutofacWebApiDependencyResolver))
            {
                configuration.DependencyResolver = new AutofacWebApiDependencyResolver();
            }
            return (AutofacWebApiDependencyResolver)configuration.DependencyResolver;
        }
    }
}
